<?php
/**
 * Created by PhpStorm.
 * User: xiongrui
 * Date: 2018-03-08
 * Time: 11:52
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\Goodsmodel;

class Company extends Admin
{
    public function _initialize() {
        parent::_initialize();
        $this->Company = model('Company');
    }

    public function index() {
        $map  = array('status' => array('gt', -1));
        $list = $this->Company->where($map)->order('id desc')->column('*', 'id');
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->assign('list', $list);
        $this->setMeta('企业');
        return $this->fetch();
    }

    public function add() {
        if (IS_POST) {

            $result = $this->Company->change();
            $this->qrcode($result['id']);
            $goodsmodel = Goodsmodel::create(['name'=>$result['title'], 'company_id'=>$result['id'], 'price_type'=>$result['price_type']]);
            $result->save(['goodsmodel_id'=>$goodsmodel->id]);

            if($result){
                $this->success('添加成功！',  url('admin/Company/index'));
            }else{
                $this->error('添加失败！');
            }
        } else {
            $data = array(
                'info'    => array('status' => 1,'sort'=>1),
                'keyList' => $this->Company->addfield(),
            );
            $this->assign($data);
            $this->setMeta("添加分类");
            return $this->fetch('public/edit');
        }
    }

    public function edit($id) {
        if (!$id) {
            return $this->error("非法操作！");
        }
        if (IS_POST) {
            $result = $this->Company->change();
            $result = $this->Company->find($result->id);
            $this->qrcode($id);
            Goodsmodel::update(['name'=>$result['title'], 'company_id'=>$result['id'], 'price_type'=>$result['price_type']], ['id'=>$result->goodsmodel_id]);

            if ($result) {
                return $this->success("编辑成功！", url('admin/Company/index'));
            } else {
                return $this->error("编辑失败！");
            }
        } else {
            $info = $this->Company->where(array('id' => $id))->find();
            $data = array(
                'info'    => $info,
                'keyList' => $this->Company->addfield(),
            );
            $this->assign($data);
            $this->setMeta('编辑分类');
            return $this->fetch('public/edit');
        }
    }

    public function del() {
        $id = $this->getArrayParam('id');
        if (empty($id)) {
            return $this->error("非法操作！");
        }
        $result = $this->Company->where(array('id' => array('IN', $id)))->delete();
        if ($result) {
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    }

    public function qrcode($company_id){
      $path = 'uploads/QrcPic/';
      $fileName = $path.'qiye_'.$company_id.'.png';
      $url =  "http://www.xgw0851.com/index/index/qiye/cid/{$company_id}";
      $this->createQRcode($url, $fileName);
    }

    public function createQRcode($url,$fileName, $level ='L', $size="10"){
          vendor("phpqrcode.phpqrcode");
          if(!file_exists($fileName)){
              \QRcode::png($url, $fileName, $level, $size);
          }
      }

}
